<template>
  <div>
    <!-- 搜索结果页-头部导航 -->
    <div class="search-result-container">
      <!-- 点击实现后退效果 -->
      <van-nav-bar
        title="搜索结果"
        left-arrow
        @click-left="$router.go(-1)"
        fixed
      />
    </div>
    <!-- 搜索结果页面 -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <ArticleItem
        v-for="obj in searchArticlesList"
        :key="obj.art_id"
        :articlesList="obj"
        :isShow="false"
        @click.native="articleClickFn(obj.art_id)"
      ></ArticleItem>
    </van-list>
  </div>
</template>

<script>
import ArticleItem from '@/components/ArticleItem'
import { searchResultListAPI } from '@/api'
export default {
  name: 'SearchResult',
  components: {
    ArticleItem
  },
  created () {
    this.getListFn()
  },
  data () {
    return {
      searchArticlesList: [],
      loading: false,
      finished: false,
      page: 1
    }
  },
  methods: {
    onLoad () {
      this.getListFn()
      // 页码加一
      // this.page++
      this.loading = false
    },
    // 请求搜索文章列表结果
    async getListFn () {
      const res = await searchResultListAPI({
        keywords: this.$route.params.keywords,
        page: ++this.page // 拿到关键词, keywords来源于router/index.js定义的动态路由参数名
      })
      if (res.data.data.results === null || res.data.data.results.length === 0) {
        this.finished = true
      }
      // console.log(res)
      this.searchArticlesList = [...this.searchArticlesList, ...res.data.data.results]
    },
    // 文章单元-点击事件
    articleClickFn (artId) {
      this.$router.push({
        path: '/art_detail',
        query: {
          aid: artId // 文章id传到文章详情页
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.search-result-container {
  padding-top: 46px;
}
</style>
